package sfsoftware.game.actor.enemy;

import sfsoftware.game.actor.attributes.Poisonable;
import sfsoftware.game.trevor.Enemy;
import sfsoftware.util.Clock;
import sfsoftware.util.Sound;
import sfsoftware.util.Tile;
import sfsoftware.util.Updatable;

public class SimpleShooter extends Enemy implements Updatable, Poisonable {
	protected boolean firingBullet = false;
	protected int firingCountdown = 0;
	protected boolean isPoisoned = false;	
	
	protected Clock animationTimer = new Clock(300);
	protected Clock firingCountdownClock = new Clock(150);
	
	protected int shootFrequency = 1;
	
	public SimpleShooter(int destX, int destY, String texture, int destWidth,
			int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, destWidth, destHeight);
		tileMap = assocTileMap;
		health = 4;
		maxHealth = 4;
		weight = 1.75f;
		setSpeed(1);
		direction = changeDirection();
	}

	public SimpleShooter(int destX, int destY, String texture, int srcX,
			int srcY, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		tileMap = assocTileMap;
		health = 4;
		maxHealth = 4;
		weight = 1.75f;
		setSpeed(1);
		direction = changeDirection();
	}

	@Override
	public void draw()
	{
		if(firingBullet)
		{	
			if(firingCountdown % 2 == 0)
			{
				if(!isPoisoned)
				{
					super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, 0.75f, 0, 0, 1);
				}
				else
				{
					super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, 0.75f, 1, 0, 1);
				}
			}
			else
			{
				if(!isPoisoned)
				{
					super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, 1, 1, 1, 1);										
				}
				else
				{
					super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, 0, 1, 0, 1);
				}
			}
		}
		else
		{
			if(!isPoisoned)
			{
				super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, 1, 1, 1, 1);					
			}
			else
			{
				super.draw(width, height, this.getSrcX(), this.getSrcY(), width, height, 0, 1, 0, 1);
			}
		}
	}
	
	@Override
	public void setPoisoned(boolean p) {
		isPoisoned = p;
		// Being poisoned causes a shooter to be way less likely to shoot!
		if(isPoisoned)
		{
			shootFrequency = 7;
		}
		else
		{
			shootFrequency = 1;
		}
	}

	@Override
	public void togglePoisoned() {
		if(this.isPoisoned)
		{
			setPoisoned(false);
		}
		else
		{
			setPoisoned(true);
		}
	}

	@Override
	public void update() {	
		
		if(!(isInvulnerable()))
		{			
			int shootOpportunity = ((int)(Math.random() * 150))  * shootFrequency;
			if(shootOpportunity == 0)
			{
				firingBullet = true;
			}
			
			if(!firingBullet)
			{
				switch(direction)
				{
					case 0:
						move(0, 1, tileMap);
						break;
					case 1:
						move(0, -1, tileMap);
						break;
					case 2:				
						move(1, 0, tileMap);
						break;
					case 3:
						move(-1, 0, tileMap);
						break;				
				}
			}
		}

		if(firingCountdownClock.updateClock())
		{
			if(firingBullet)
			{
				firingCountdown++;
				if(firingCountdown > 4)
				{
					// Here is where we create a new bullet!
					new Bullet((int)getX() + (width / 2) - 4, (int) getY() + (height / 2) - 4, "orb", 8, 8, tileMap);
					Sound.playSound("shooter");
					firingCountdown = 0;
					firingBullet = false;
				}
			}
		}		
		
		if(animationTimer.updateClock())
		{
			if(getSrcX() == 0)
			{
				setSrcX(20);
			}
			else
			{
				setSrcX(0);
			}
		}
	}
	
	public boolean move(int dirX, int dirY, Tile[][] tileMap)
	{		
		boolean successfulMove = false;
		try
		{
			// Try to move!
			successfulMove = super.move(dirX, dirY, tileMap); 
			if(!successfulMove)
			{				
				// If the move was unsuccessful, change direction
				direction = changeDirection();
			}
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			// If the actor tried to move off the playing field, change direction!			
			direction = changeDirection();
		}
		return successfulMove;
	}	
	
	private int changeDirection()
	{
		// If they are going up or down, they change direction to left or right
		if(direction == UP_DIRECTION  ||  direction == DOWN_DIRECTION)
		{
			 return (int)(Math.random() * 2) + 2;
		}
		// Otherwise, they change direction to up or down... 
		// ...makes them seem smarter than they are ;)
		return  (int)(Math.random() * 2);	
	}
}
